package flyln.uestc.edu.a2048;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Created by asus on 2017/8/27.
 */

public class Card extends FrameLayout {



    private TextView tvNum;//数字文本框
//    private View background;//背景色
    private int num = 0;//数字



    public Card(@NonNull Context context) {
        super(context);


        tvNum = new TextView(getContext());
        tvNum.setTextSize(32);
        tvNum.setGravity(Gravity.CENTER);
        tvNum.setBackgroundColor(0x33ffffff);


        LayoutParams layoutParams = new LayoutParams(-1,-1);
        layoutParams.setMargins(10,10,0,0);
        this.addView(tvNum,layoutParams);

        setNum(0);


    }





    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;


        switch (this.num){
            case 0:
                tvNum.setBackgroundResource(R.color.bg0);
                break;
            case 2:
                tvNum.setBackgroundResource(R.color.bg2);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text2));
                break;
            case 4:
                tvNum.setBackgroundResource(R.color.bg4);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text4));
                break;
            case 8:
                tvNum.setBackgroundResource(R.color.bg8);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text8));
                break;
            case 16:
                tvNum.setBackgroundResource(R.color.bg16);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text16));
                break;
            case 32:
                tvNum.setBackgroundResource(R.color.bg32);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text32));
                break;
            case 64:
                tvNum.setBackgroundResource(R.color.bg64);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text64));
                break;
            case 128:
                tvNum.setBackgroundResource(R.color.bg128);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text128));
                break;
            case 256:
                tvNum.setBackgroundResource(R.color.bg256);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text256));
                break;
            case 512:
                tvNum.setBackgroundResource(R.color.bg512);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text512));
                break;
            case 1024:
                tvNum.setBackgroundResource(R.color.bg1024);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text1024));
                break;
            case 2048:
                tvNum.setBackgroundResource(R.color.bg2048);
                tvNum.setTextColor(ContextCompat.getColor(getContext(),R.color.text2048));
                break;
            default:
        }

        if (num > 0)
            tvNum.setText(num + "");
        else
            tvNum.setText("");
    }
    public boolean equals(Card card){
        if (card.getNum() == getNum())
            return true;
        else
            return false;
    }

    public TextView getTvNum() {
        return tvNum;
    }
}
